import { Component, OnInit, Input, ViewChild, Output, EventEmitter } from '@angular/core';
import { Zone, ZoneDetail, ZoneService } from '../../services/zone.service';
import { NgForm, FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
	selector: 'm-info-widget',
	templateUrl: './info-widget.component.html',
	styleUrls: ['./info-widget.component.scss'],
})
export class InfoWidgetComponent implements OnInit {
	form = new FormGroup({
		id: new FormControl(0, Validators.required),
		name: new FormControl('', Validators.required),
		des: new FormControl(''),
	});
	@Input() zone: ZoneDetail;
	@Output() changed = new EventEmitter();
	edit = false;

	constructor(private zoneService: ZoneService) {}

	ngOnInit() {
		this.form.patchValue(this.zone);
	}

	async submit() {
		if (this.form.invalid) {
			return;
		}
		await this.zoneService.update(this.form.value);
		this.edit = false;
		this.changed.emit();
	}
}
